package com.wedu.modules.test.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wedu.common.utils.PageUtils;
import com.wedu.common.utils.R;
import com.wedu.modules.sys.controller.AbstractController;
import com.wedu.modules.sys.dao.SysUserDao;
import com.wedu.modules.sys.entity.SysUserEntity;
import com.wedu.modules.test.entity.StudentScoreEntity;
import com.wedu.modules.test.service.StudentScoreService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 学生成绩
 */
@RestController
@RequestMapping("test/studentScore")
public class StudentScoreController extends AbstractController {
    @Autowired
    private StudentScoreService studentScoreService;
    @Autowired
    private SysUserDao sysUserDao;

    /**
     * 学生分页列表
     */
    @RequestMapping("list")
    @RequiresPermissions("test:studentscore:list")
    public R list(@RequestParam Map<String,Object> params){
        Long userId = getUserId();//获取当前登录对象对象的Id
        PageUtils page=studentScoreService.queryPage(params,userId);

        QueryWrapper<SysUserEntity> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.eq("user_id", userId);//查询是学生的用户信息
        SysUserEntity user = sysUserDao.selectOne(userQueryWrapper);
        Integer isStudent = user.getIsStudent();

        R r = R.ok().put("page", page);
        r.put("isStudent",isStudent);
        return r;

    }

    /**
     * 学生用户列表（用于录入学生成绩）
     */
    @RequestMapping("studentList")
    @RequiresPermissions("test:studentscore:list")
    public R studentList(){
       List<SysUserEntity> studentList =studentScoreService.queryStudentList();


        return R.ok().put("studentList",studentList);

    }
    /**
     * 按照id查询进行修改
     */
    @RequestMapping("info/{id}")
    @RequiresPermissions("test:studentscore:info")
    public R info(@PathVariable("id") Long id){
        StudentScoreEntity studentScore=studentScoreService.getById(id);

        return R.ok().put("studentScore",studentScore);

    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("test:studentscore:save")
    public R save(@RequestBody StudentScoreEntity studentScore){
        studentScore.setCreateTime(new Date());
        studentScore.setCreateBy(getUserId());
        studentScoreService.save(studentScore);

        return R.ok();

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("test:studentscore:update")
    public R update(@RequestBody StudentScoreEntity studentScore){
        studentScore.setUpdateTime(new Date());
        studentScore.setUpdateBy(getUserId());
        studentScoreService.updateById(studentScore);

        return R.ok();

    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("test:studentscore:delete")
    public R delete(@RequestBody Long[] ids){
        studentScoreService.removeByIds(Arrays.asList(ids));

        return R.ok();

    }


}
